/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * addCliente.java
 *
 * Created on Apr 29, 2010, 2:25:55 PM
 */

package UI;

import Excepciones.BasesdeDatos;
import java.sql.SQLException;
import robot_maker.*;
import Excepciones.ClienteException;
import Excepciones.ProcesamientoDato;
import Excepciones.Sistema;



/**
 *
 * @author CarlosScipione
 */
public class addCliente extends javax.swing.JFrame {

    private UI_Clientes owner;
    private boolean modify;
    private int idCi;

    /** Creates new form addCliente */
    public addCliente() {
        initComponents();
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);
    }

    addCliente(UI_Clientes aThis, boolean mod, int id) {
        initComponents();
        owner = aThis;
        modify = mod;
        idCi = id;
        this.setDefaultCloseOperation(HIDE_ON_CLOSE);

         if(mod){
             agregar.setText("Guardar");
            Cliente datos = new Cliente();
            try {
                try {
                    datos = datos.getDatos(id);
                } catch (ProcesamientoDato ex) {
                }

            } catch (Exception ex) {
                Sistema.loggear(4, "CONTRUCTOR PANTALLA",
                        "NO SE PUDO TRAER DATOS DE LA BASE DE DATOS", 
                        addCliente.class, "SQLException");
            }
            ciRif_Text.setText(String.valueOf(datos.getCiRif()));
            nombre_text.setText(datos.getNombre());
            apellido_text.setText(datos.getApellido());
            telefono_text.setText(String.valueOf(datos.getTelefono()));
            calle_text.setText(datos.getCalle());
            sexo_combo.setSelectedItem(datos.getSexo());
            estadoCivil_combo.setSelectedItem(datos.getEstadoCivil());
            estado_combo.setSelectedItem(datos.getEstado());
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        title = new javax.swing.JLabel();
        ciRif_Text = new javax.swing.JFormattedTextField();
        ciRif_label = new javax.swing.JLabel();
        nombre_label = new javax.swing.JLabel();
        nombre_text = new javax.swing.JTextField();
        apellido_label = new javax.swing.JLabel();
        telefono_label = new javax.swing.JLabel();
        sexo_label = new javax.swing.JLabel();
        estadoCivil_label = new javax.swing.JLabel();
        estado_label = new javax.swing.JLabel();
        calle_label = new javax.swing.JLabel();
        apellido_text = new javax.swing.JTextField();
        estado_combo = new javax.swing.JComboBox();
        sexo_combo = new javax.swing.JComboBox();
        calle_text = new javax.swing.JTextField();
        estadoCivil_combo = new javax.swing.JComboBox();
        agregar = new javax.swing.JButton();
        cancelIt = new javax.swing.JButton();
        telefono_text = new javax.swing.JFormattedTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        title.setText("Ingresar Cliente");

        ciRif_Text.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("#0"))));
        ciRif_Text.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ciRif_TextActionPerformed(evt);
            }
        });

        ciRif_label.setText("C.I./R.I.F.:");

        nombre_label.setText("Nombre:");

        apellido_label.setText("Apellido:");

        telefono_label.setText("Telefono:");

        sexo_label.setText("Sexo:");

        estadoCivil_label.setText("Estado Civil:");

        estado_label.setText("Estado:");

        calle_label.setText("Calle:");

        estado_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Miranda", "Bolivar", "Apure", " " }));

        sexo_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Masculino", "Femenino" }));

        estadoCivil_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Soltero", "Casado", "Divorciado", "Viudo" }));

        agregar.setText("Agregar");
        agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarActionPerformed(evt);
            }
        });

        cancelIt.setText("Cancelar");
        cancelIt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelItActionPerformed(evt);
            }
        });

        try {
            telefono_text.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###-######")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(telefono_label)
                    .add(sexo_label)
                    .add(calle_label)
                    .add(title)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(ciRif_label)
                            .add(nombre_label)
                            .add(apellido_label)
                            .add(estadoCivil_label)
                            .add(estado_label))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                        .add(calle_text, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                                        .add(telefono_text, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                                        .add(org.jdesktop.layout.GroupLayout.TRAILING, apellido_text, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 218, Short.MAX_VALUE)
                                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                            .add(cancelIt)
                                            .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 27, Short.MAX_VALUE)
                                            .add(agregar)))
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                                        .add(nombre_text)
                                        .add(ciRif_Text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 169, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                                .add(88, 88, 88))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, estado_combo, 0, 124, Short.MAX_VALUE)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, sexo_combo, 0, 124, Short.MAX_VALUE)
                                    .add(org.jdesktop.layout.GroupLayout.LEADING, estadoCivil_combo, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .add(182, 182, 182)))))
                .add(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(title)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(ciRif_label)
                    .add(ciRif_Text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(nombre_label)
                    .add(nombre_text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(apellido_label)
                    .add(apellido_text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(telefono_label)
                    .add(telefono_text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(sexo_label)
                    .add(sexo_combo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(estadoCivil_label)
                    .add(estadoCivil_combo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(estado_label)
                    .add(estado_combo, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(calle_label)
                    .add(calle_text, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(agregar)
                    .add(cancelIt))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ciRif_TextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ciRif_TextActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ciRif_TextActionPerformed

    private void agregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarActionPerformed


  Cliente consumidor = null;
        try {
            consumidor = new Cliente(
                    ciRif_Text.getText(),
                    nombre_text.getText(),
                    apellido_text.getText(),
                    telefono_text.getText(),
                    sexo_combo.getSelectedItem().toString().charAt(0),
                    estadoCivil_combo.getSelectedItem().toString(),
                    estado_combo.getSelectedItem().toString(),
                    calle_text.getText());
       

         if(modify)
             consumidor.modificarBD(consumidor, idCi);
         else
             consumidor.ingresarBD(consumidor);
       
       
     }  catch (ProcesamientoDato ex) {
        } catch (ClienteException ex) {
		

        }
        this.setVisible(false);
       owner.refrescar();
    }//GEN-LAST:event_agregarActionPerformed

    private void cancelItActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelItActionPerformed
        this.setVisible(false);
        owner.refrescar();
    }//GEN-LAST:event_cancelItActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new addCliente().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton agregar;
    private javax.swing.JLabel apellido_label;
    private javax.swing.JTextField apellido_text;
    private javax.swing.JLabel calle_label;
    private javax.swing.JTextField calle_text;
    private javax.swing.JButton cancelIt;
    private javax.swing.JFormattedTextField ciRif_Text;
    private javax.swing.JLabel ciRif_label;
    private javax.swing.JComboBox estadoCivil_combo;
    private javax.swing.JLabel estadoCivil_label;
    private javax.swing.JComboBox estado_combo;
    private javax.swing.JLabel estado_label;
    private javax.swing.JLabel nombre_label;
    private javax.swing.JTextField nombre_text;
    private javax.swing.JComboBox sexo_combo;
    private javax.swing.JLabel sexo_label;
    private javax.swing.JLabel telefono_label;
    private javax.swing.JFormattedTextField telefono_text;
    private javax.swing.JLabel title;
    // End of variables declaration//GEN-END:variables

}
